//
//  CCMMineOrderItemViewCell.swift
//  CCMoney_swift
//
//  Created by JKHuang on 2018/11/27.
//  Copyright © 2018 JKHuang. All rights reserved.
//

import UIKit

let kCCMOrderItemCollectionViewCell = "CCMOrderItemCollectionViewCell"


class CCMMineOrderItemViewCell: UITableViewCell {

    fileprivate lazy var itemCollection:UICollectionView = {
        
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.minimumInteritemSpacing = 10
        layout.sectionInset = UIEdgeInsets(top: 5, left: 10, bottom: 0, right: 10)
        layout.itemSize = CGSize(width: (ScreenWidth-48-10*3)/3, height: 80)
        let collection = UICollectionView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 80), collectionViewLayout: layout)
        collection.backgroundColor = UIColor.hex(0x2D3554)
        collection.delegate = self
        collection.dataSource = self
        collection.isScrollEnabled = false
        collection.showsVerticalScrollIndicator = false
        collection.showsHorizontalScrollIndicator = false
        collection.register(CCMOrderItemCollectionViewCell.self, forCellWithReuseIdentifier: kCCMOrderItemCollectionViewCell)
        return collection
    }()
    
    fileprivate lazy var imageArray:Array = ["待付款_icon", "待发货_icon", "已发货_icon"]
    fileprivate lazy var titleArray:Array = ["待付款", "待发货", "已发货"]
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = UIColor.hex(0x2D3554)
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension CCMMineOrderItemViewCell{
    
    fileprivate func setUI(){
        
        self.contentView.addSubview(self.itemCollection)
    }
}

extension CCMMineOrderItemViewCell:UICollectionViewDataSource, UICollectionViewDelegate{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return imageArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let item = collectionView.dequeueReusableCell(withReuseIdentifier: kCCMOrderItemCollectionViewCell, for: indexPath)as! CCMOrderItemCollectionViewCell
        item.titleLabel.text = self.titleArray[indexPath.item]
        item.iconImage.image = UIImage(named: self.imageArray[indexPath.item])
        return item
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        if indexPath.item == 0{
//            let vc = CCMMineSettingVC()
//            vc.hidesBottomBarWhenPushed = true
//            getCurrentVCBS2().navigationController?.pushViewController(vc, animated: true)
        }
    }
    
    
    func getCurrentVCBS2() -> UIViewController {
        let keywindow = (UIApplication.shared.delegate as! AppDelegate).window?.rootViewController//UIApplication.shared.keyWindow
        let rootVC = keywindow!//UIApplication.shared.keyWindow!.rootViewController!
        return getVisibleViewControllerFrom(vc: rootVC)
            
    }
    
    func getVisibleViewControllerFrom(vc: UIViewController) -> UIViewController {
        if vc.isKind(of: UINavigationController.self) {
            return getVisibleViewControllerFrom(vc: (vc as! UINavigationController).visibleViewController!)
        } else if vc.isKind(of: UITabBarController.self) {
            return getVisibleViewControllerFrom(vc: (vc as! UITabBarController).selectedViewController!)
        } else {
            if (vc.presentedViewController != nil) {
                return getVisibleViewControllerFrom(vc: vc.presentedViewController!)
            } else {
                return vc
            }
        }
    }
}


